# encoding: utf-8
"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by lei.chang on '18/12/2024'
    comment: 乐城服务相关
"""
import asyncio
import aiohttp

from config import Config
from project.lib.common import logger
from project.core.exception import BizError


class HomeCare:

    @staticmethod
    def get_headers(device_id=None):
        """
        :return:
        """
        headers = {
            'Content-Type': 'application/json'
        }
        if device_id is not None:
            headers.update({'deviceId': device_id})

        return headers

    @staticmethod
    async def get_scheduling_day(device_id, month, service_name):
        """ 得到月度排班信息
        :param device_id: 
        :param month:
        :param service_name: hospitalize(陪同就医)/cleaning(保洁)
        :return: 
        """
        url = f'{Config.HOME_CARE}/health-care-api/business-api/cariabot/homecare/getServiceAvailableDateList'

        params = {
            'query_date': month,
            'service_name': service_name
        }

        async with aiohttp.ClientSession() as session:
            async with session.post(url, headers=HomeCare.get_headers(device_id=device_id), json=params) as resp:
                if resp.status != 200:
                    error = f'获取排班信息失败 status: {resp.status} content: {(await resp.content.read())}'
                    logger.error(message=error)
                    raise BizError(message=error)

                result = await resp.json()
                if result.get('resultCode') != 200:
                    error = f'获取排班信息失败 code: {result.get("resultCode")} message: {result.get("message")}'
                    logger.error(message=error)
                    raise BizError(message=error)

        return result.get('result', {}).get('schedule', [])

    @staticmethod
    async def get_scheduling_time_period(device_id, start_day, end_day, service_name):
        """ 得到日期的时段排班信息
        :param device_id:
        :param start_day:
        :param end_day:
        :param service_name:
        :return:
        """
        url = f'{Config.HOME_CARE}/health-care-api/business-api/cariabot/homecare/getServiceAvailableTimeList'

        params = {
            'query_date': start_day,
            'query_end_date': end_day,
            'service_name': service_name
        }

        async with aiohttp.ClientSession() as session:
            async with session.post(url, headers=HomeCare.get_headers(device_id=device_id), json=params) as resp:
                if resp.status != 200:
                    error = f'获取时段排班信息失败 status: {resp.status} content: {(await resp.content.read())}'
                    logger.error(message=error)
                    raise BizError(message=error)

                result = await resp.json()
                if result.get('resultCode') != 200:
                    error = f'获取时段排班信息失败 code: {result.get("resultCode")} message: {result.get("message")}'
                    logger.error(message=error)
                    raise BizError(message=error)

        return result.get('result', {}).get('schedule', [])


if __name__ == "__main__":

    result = asyncio.run(HomeCare.get_scheduling_time_period('dd3fa9483a3c166d', '2025-03-14', 'hospitalize'))
    print(result)